package com.js.admin.controller;

import com.js.admin.entity.BaseDepart;
import com.js.admin.entity.BaseUser;
import com.js.admin.service.BaseDepartService;
import com.js.admin.vo.DepartTree;
import com.js.auth.client.annotation.CheckUserToken;
import com.js.common.controller.BaseController;
import com.js.common.response.ObjectRestResponse;
import com.js.common.response.TableResultResponse;
import com.js.common.util.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author： ygl
 * @date： 2018/11/06-14:40
 * @Description：
 */
@CheckUserToken
@Api(tags = {"部门模块"})
@RestController
@RequestMapping("/depart")
public class BaseDepartController extends BaseController<BaseDepart,String>{

    @Autowired
    private BaseDepartService baseDepartService;

    @ApiOperation("获取部门树")
    @GetMapping("/tree")
    public List<DepartTree> getTree() {
        List<BaseDepart> departs = baseDepartService.selectAll();
        List<DepartTree> trees = new ArrayList<>();
        departs.forEach(dictType -> {
            trees.add(new DepartTree(dictType.getId(), dictType.getParentId(), dictType.getName(),dictType.getCode()));
        });
        return TreeUtil.bulid(trees, "-1", null);
    }
    @ApiOperation("获取部门关联用户")
    @GetMapping("/user")
    public TableResultResponse<BaseUser> getDepartUsers(String departId, String userName){
        return baseDepartService.getDepartUsers(departId,userName);
    }

    @ApiOperation("部门添加用户")
    @PostMapping("/user")
    public ObjectRestResponse<Boolean> addDepartUser(String departId, String userIds){
        baseDepartService.addDepartUser(departId,userIds);
        return new ObjectRestResponse<>().data(true);
    }

    @ApiOperation("部门移除用户")
    @DeleteMapping("/user")
    public ObjectRestResponse<Boolean> delDepartUser(String departId,String userId){
        baseDepartService.delDepartUser(departId,userId);
        return new ObjectRestResponse<>().data(true);
    }

    @ApiOperation("获取部门信息")
    @GetMapping("getByPK/{id}")
    public Map<String,String> getDepart(@PathVariable String id){
        return baseDepartService.getDeparts(id);
    }


}
